package com.prs.crm.action.approval;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.approval.JobType;
import com.prs.crm.domain.approval.ShenQingType;
import com.prs.crm.domain.document.DraftType;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.approval.JobTypeService;
import com.prs.crm.service.approval.ShenQingTypeService;

public class ShenQingTypeAction extends BaseAction{
	private ShenQingTypeService shenQingTypeService;
	private ShenQingType shenQingType;
	private List<JobType> jobtypes;
	private JobTypeService jobTypeService;
	private PaginationSupport pagination;
	private List<ShenQingType> shenQingTypes;
	private Integer[] ids;
	private Collection<TreeNode> nodes;
	public Collection<TreeNode> getNodes() {
		return nodes;
	}
	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}
	public ShenQingTypeService getShenQingTypeService() {
		return shenQingTypeService;
	}
	public void setShenQingTypeService(ShenQingTypeService shenQingTypeService) {
		this.shenQingTypeService = shenQingTypeService;
	}
	public ShenQingType getShenQingType() {
		return shenQingType;
	}
	public void setShenQingType(ShenQingType shenQingType) {
		this.shenQingType = shenQingType;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public List<JobType> getJobtypes() {
		return jobtypes;
	}
	public void setJobtypes(List<JobType> jobtypes) {
		this.jobtypes = jobtypes;
	}
	public JobTypeService getJobTypeService() {
		return jobTypeService;
	}
	public void setJobTypeService(JobTypeService jobTypeService) {
		this.jobTypeService = jobTypeService;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public List<ShenQingType> getShenQingTypes() {
		return shenQingTypes;
	}
	public void setShenQingTypes(List<ShenQingType> shenQingTypes) {
		this.shenQingTypes = shenQingTypes;
	}
	/*****************************************************************************/
	
	
	
	
	
	/*****************查询所有模板*******************/
	@Action("listShenQingType")
	public String listAll(){
		Person per=AppContext.getAccountContext().currentUser();
		List<ShenQingType> dt=this.getShenQingTypeService().getAllShenQingTypes();
		for (ShenQingType dts : dt) {
			if (dts.getWeihu()==null) {
				dts.setUpState(5);
				this.getShenQingTypeService().submit(dts);
			}else{
				String[] wh=dts.getWeihu().split(",");
				for (String whs : wh) {
					if (per.getResume().getName().equals(whs)) {
						dts.setUpState(0);
						this.getShenQingTypeService().submit(dts);
						break;
					}else{
						dts.setUpState(5);
						this.getShenQingTypeService().submit(dts);
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(ShenQingType.class);
		criteria.add(Restrictions.eq("upState", 0));
		this.setPagination(this.getShenQingTypeService().listByPage(criteria, 
				this.getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	/*********************新增**********************/
	@Action(value = "saveShenQingType", results = {
			@Result(name = "success", type = "redirect", location = "listShenQingType"),
			@Result(name = "error", type = "chain", location = "editShenQingType") })
	public String saveShenQingType(){
		if (shenQingType.getWeihu()==null || "".equals(shenQingType.getWeihu().trim()) || "0".equals(shenQingType.getWeihu().trim())) {
			shenQingType.setWeihu(AppContext.getAccountContext().currentUser().getResume().getName());
		}
		this.shenQingTypeService.save(shenQingType);
		return SUCCESS;
	}
	////模板类型////
	@Action("editShenQingType")
	public String editShenQingType(){
		jobtypes = (List<JobType>) this.jobTypeService.getRootJobTypes();
		return SUCCESS;
	}
	//**********删除******//
	@Action(value = "delShenQingType", results = {
			@Result(name = "success", type = "redirect", location = "listShenQingType"),
			@Result(name = "error", type = "chain", location = "listShenQingType") })
	public String delShenQingType(){
		this.shenQingTypeService.batchRemove(ids);
		return SUCCESS;
	}
	//循环读取菜单
	@Action("Navigation")
	public String Navigation(){
		jobtypes = (List<JobType>) this.jobTypeService.getRootJobTypes();
		shenQingTypes = this.shenQingTypeService.getAllShenQingType();
//		for (int i = 0; i < jobtypes.size(); i++) {
//			List<ShenQingType> job+i 
//			
//		}
		return SUCCESS;
	}
	
	@Action(value = "loadShengQingTypeTree", results = { @Result(name = "success", type = "json",
			params = {"root", "nodes" }) })
	public String loadShengQingTypeTree(){
		nodes = new ArrayList<TreeNode>();
		Collection<JobType> jobTypes = this.getJobTypeService().getRootJobTypes();
		for (JobType job : jobTypes) {
			TreeNode node = new TreeNode();
			node.setId(String.valueOf(job.getId()));
			node.setText(job.getName());
			nodes.add(node);
			addJobTypeNode(job, node);
//			addJobTypesNode(node, job);
		}
		return SUCCESS;
	}
//	private void addJobTypesNode(TreeNode parent, JobType jobType) {
//		for (JobType jt: jobType.getChilds()) {
//			TreeNode node = new TreeNode();
//			node.setText(jt.getName());
//			node.setId(String.valueOf(jt.getId()));
//			parent.addChildNode(node);
//			addJobTypesNode(node, jt);
//			addJobTypeNode(jt, node);
//		}
//	}

	private void addJobTypeNode(JobType jobType,TreeNode parent){
		for (ShenQingType shenQingType : this.getShenQingTypeService().findByJobType(jobType)) {
			TreeNode node=new TreeNode();
			if (shenQingType.getJobType()!=null) {
				node.setText(shenQingType.getName());
			}
			node.setId(String.valueOf(shenQingType.getJobType().getId()));
			parent.addChildNode(node);
//			addJobTypeNode(jobType, node);
		}
	}
}
